package cn.lyq.system.domain.asset.asset.events;

import cn.lyq.system.domain.asset.asset.Asset;
import cn.lyq.system.domain.asset.asset.domainservice.model.AssetBizInfo;
import cn.lyq.system.domain.asset.assetlifecycle.creator.AssetLifecycleCreator;
import cn.lyq.system.domain.asset.assetlifecycle.service.IAssetLifecycleService;
import cn.lyq.system.domain.asset.assetrecord.InOutType;
import cn.lyq.system.domain.asset.assetrecord.creator.AssetInOutRecordCreator;
import cn.lyq.system.domain.asset.assetrecord.service.IAssetInOutRecordService;
import cn.lyq.system.domain.asset.warehouse.service.IWarehouseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
@RequiredArgsConstructor
public class AssetEventProcessor {
    private final IAssetLifecycleService assetLifecycleService;
    private final IWarehouseService warehouseService;

    private final IAssetInOutRecordService assetInOutRecordService;

    /**
     * 入库操作
     *
     * @param assetInEvent
     */
    @EventListener
    public void handAssetInForLifecycle(AssetEvents.AssetInEvent assetInEvent) {
        AssetBizInfo bizInfo = assetInEvent.getAssetBizInfo();
        Asset asset = assetInEvent.getAsset();
        List<AssetLifecycleCreator> lifecycleInfo = bizInfo.getUniqueCodes().stream().map(
                code -> {
                    AssetLifecycleCreator creator = new AssetLifecycleCreator();
                    creator.setAssetsId(asset.getId());
                    creator.setHouseId(asset.getHouseId());
                    creator.setName(asset.getName());
                    //不用关注findById 那是仓库服务应该考虑的事，最少知道原则，那个优化可以加缓存
                    creator.setHouseName(warehouseService.findById(asset.getHouseId()).getName());
                    creator.setInOutBizType(bizInfo.getInOutBizType());
                    creator.setUniqueCode(code);
                    creator.setRemark("入库");
                    creator.setBatchNo(bizInfo.getBatchNo());
                    creator.setGenBatchNo(bizInfo.getGenBatchNo());
                    creator.setInOutType(InOutType.IN);
                    creator.setSkuId(asset.getSkuId());
                    creator.setOperateUser(bizInfo.getOperateUser());
                    return creator;
                }).collect(Collectors.toList());
        assetLifecycleService.batchCreateLifecycle(bizInfo.getBatchNo(), lifecycleInfo);
    }

    /**
     * 出库操作
     *
     * @param assetOutEvent
     */
    @EventListener
    public void handleAssetOutForLifecycle(AssetEvents.AssetOutEvent assetOutEvent) {
        AssetBizInfo bizInfo = assetOutEvent.getAssetBizInfo();
        Asset asset = assetOutEvent.getAsset();
        List<AssetLifecycleCreator> assetLifecycleCreators = bizInfo.getUniqueCodes().stream()
                .map(code -> {
                    AssetLifecycleCreator creator = new AssetLifecycleCreator();
                    creator.setAssetsId(asset.getId());
                    creator.setHouseId(asset.getHouseId());
                    creator.setName(asset.getName());
                    //不用关注findById 那是仓库服务应该考虑的事，最少知道原则，那个优化可以加缓存
                    creator.setHouseName(warehouseService.findById(asset.getHouseId()).getName());
                    creator.setInOutBizType(bizInfo.getInOutBizType());
                    creator.setUniqueCode(code);
                    creator.setRemark("出库");
                    creator.setBatchNo(bizInfo.getBatchNo());
                    creator.setSkuId(asset.getSkuId());
                    creator.setGenBatchNo(bizInfo.getGenBatchNo());
                    creator.setInOutType(InOutType.OUT);
                    creator.setOperateUser(bizInfo.getOperateUser());
                    return creator;
                }).collect(Collectors.toList());
        assetLifecycleService.batchCreateLifecycle(bizInfo.getGenBatchNo(), assetLifecycleCreators);
    }

    /**
     * 出库记录
     *
     * @param assetOutEvent
     */
    @EventListener
    public void handleAssetOutRecord(AssetEvents.AssetOutEvent assetOutEvent) {
        AssetBizInfo bizInfo = assetOutEvent.getAssetBizInfo();
        AssetInOutRecordCreator creator = new AssetInOutRecordCreator();
        creator.setInOutBizType(bizInfo.getInOutBizType());
        creator.setInOutType(InOutType.OUT);
        creator.setBatchNo(bizInfo.getBatchNo());
        creator.setGenBatchNo(bizInfo.getGenBatchNo());
        creator.setOperateUser(bizInfo.getOperateUser());
        creator.setTotalCount(bizInfo.getUniqueCodes().size());
        assetInOutRecordService.createAssetInOutRecord(bizInfo.getUniqueCodes(), creator);
    }

    /**
     * 入库记录
     *
     * @param assetInEvent
     */
    @EventListener
    public void handleAssetInRecord(AssetEvents.AssetInEvent assetInEvent) {
        AssetBizInfo assetBizInfo = assetInEvent.getAssetBizInfo();

    }
}
